from day15.student_model import StudentModel


class StudentManager(object):
    def __init__(self):
        self.students=[]
        self.next_sid=1

    def add_student(self,name,gender,age,s_class,email,dress)->None:
        """
        学生的添加逻辑
        :param name:
        :param gender:
        :param age:
        :param s_class:
        :param email:
        :param dress:
        :return:
        """
        # 将 self.next_sid 赋值给 sid
        sid=self.next_sid
        # 学生模型的实例化
        student=StudentModel(sid,name,gender,age,s_class,email,dress)
        # 把学生实例添加到列表中
        self.students.append(student)
        # 自增id +1
        self.next_sid+=1

    def valid_check(self,name,gender,age,s_class,email,dress):
        """
        进行输入项的校验
        :param name:
        :param gender:
        :param age:
        :param s_class:
        :param email:
        :param dress:
        :return:
        """
        if not all([name, gender, age, s_class, email, dress]):
            # 避免写成 if not (name and gender and age and s_class and email and addr )
            return "所有信息都必须填写"
        try:
            int_age = int(age)
            if int_age < 0 or int_age > 200:
                return "用户输入的年龄超出限制"
        except  Exception as e:
            return "用户输入的年龄格式不正确"
        return None


    def find_user(self,search_name):
        """
        实现查找功能
        :param search_name:
        :return:
        """
        new_students=[]
        for stu in self.students:
            if search_name in stu.name:
                new_students.append(stu)
        return new_students


    def update_student(self,sid,name,gender,age,s_class,email,dress):
        """
        实现修改功能
        :param sid:
        :param name:
        :param gender:
        :param age:
        :param s_class:
        :param email:
        :param dress:
        :return:
        """
        student= self.get_student(sid)
        if student:
            student.name=name
            student.gender=gender
            student.age=age
            student.s_class=s_class
            student.email=email
            student.dress=dress


    def get_student(self,sid):
        student=next((stu for stu in self.students if stu.sid == sid), None)
        return student


    def delete_student(self,sid):
        self.students = [stu for stu in self.students if stu.sid != sid]

